from __future__ import annotations

import argparse
import json

from iqg.orchestrator import QuestionGenerator
from iqg.history import HistoryStore


def parse_args() -> argparse.Namespace:
    p = argparse.ArgumentParser(description="Intelligent Question Generator CLI")
    p.add_argument("--name", required=True, help="知识点名称")
    p.add_argument("--desc", required=True, help="知识点描述")
    p.add_argument("--kp_difficulty", type=int, default=5, help="知识点难度(1-10)")
    p.add_argument("--cognitive", default="应用", help="认知层次")

    p.add_argument("--qtype", required=True, help="题型")
    p.add_argument("--difficulty", default="中等", help="生成难度(容易/中等/困难或1-10)")
    p.add_argument("--count", type=int, default=5, help="题目数量(<=10)")
    p.add_argument("--language", default="中文")
    p.add_argument("--include_explanation", action="store_true")
    p.add_argument("--options_count", type=int, default=4)
    p.add_argument("--seed", type=int, default=42)
    p.add_argument("--save", action="store_true", help="保存历史记录")
    return p.parse_args()


def main() -> None:
    args = parse_args()

    kp = {
        "name": args.name,
        "description": args.desc,
        "difficulty_level": args.kp_difficulty,
        "cognitive_level": args.cognitive,
    }
    cfg = {
        "question_type": args.qtype,
        "difficulty": str(args.difficulty),
        "count": args.count,
        "language": args.language,
        "format_spec": {
            "include_explanation": args.include_explanation,
            "options_count": args.options_count,
        },
    }

    qg = QuestionGenerator(seed=args.seed)
    output = qg.generate_questions(kp, cfg)

    print(json.dumps(output, ensure_ascii=False, indent=2))

    if args.save:
        store = HistoryStore()
        path = store.save_json(output)
        print(f"Saved to {path}")


if __name__ == "__main__":
    main()